/*
 * 文件名：ReflectUtil.java
 * 版权：(C)版权所有2013-2013 zilla
 * 描述：反射工具类
 * 修改人：ze.chen
 * 修改时间：2013-6-6 上午10:04:32
 */
package com.zilla.android.zillacore.libzilla.util;

import com.github.snowdream.android.util.Log;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 反射工具类<br>
 * 处理方法、字段等的反射
 *
 * @author ze.chen
 * @version 1.0
 * @date 2013-6-6 上午10:04:32
 */
public class ReflectUtil {

    public static Object invokeMethod(String className, String methodName, Class[] properties, Object... values) {
        try {
            Class<?> c = Class.forName(className);
            Method method = c.getMethod(methodName, properties);
            return method.invoke(c, values);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 反射属性
     *
     * @param obj
     * @param fieldName
     * @return
     */
    public static Object getObj(Object obj, String fieldName) {
        Object result = null;
        try {
            Field field = obj.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            result = field.get(obj);
        } catch (NoSuchFieldException e) {
            Log.e(e.getMessage());
        } catch (IllegalAccessException e) {
            Log.e(e.getMessage());
        }
        return result;
    }

    /**
     * 一个参数的构造函数
     * @param obj
     * @return
     */
    public static Object getConstructorInstance(Class c, Object obj) {
        try {
            Constructor ctor = c.getDeclaredConstructor(obj.getClass());
            return ctor.newInstance(obj);
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @Title: getFields
     * @Description: 获取ben的所有私有属性值
     * @param @param obj
     * @return String[]
     */
    public static String[] getFields(Object obj) {
        Field[] fields = obj.getClass().getDeclaredFields();
        String[] results = new String[fields.length];
        for (int i = 0, l = fields.length; i < l; i++) {
            results[i] = fields[i].getName();
        }
        return results;
    }

    /**
     *
     * @Title: getFields
     * @Description: 获取ben的所有私有属性值
     * @param @param obj
     * @return String[]
     */
//    public static String[] getFields(Class c) {
//        Field[] fields = c.getDeclaredFields();
//        String[] results = new String[fields.length];
//        for (int i = 0, l = fields.length; i < l; i++) {
//            results[i] = fields[i].getName();
//        }
//        return results;
//    }

    /**
     *
     * @Title: setFieldValue
     * @Description: 设置私有字段的值
     * @param obj
     * @param key
     * @return void
     */
    public static void setFieldValue(Object obj, String key, Object value) {
        Field field = null;
        try {
            field = obj.getClass().getDeclaredField(key);
            field.setAccessible(true);
            field.set(obj, value);
        } catch (NoSuchFieldException e) {
            Log.e(e.getMessage());
        } catch (IllegalArgumentException e) {
//            e.printStackTrace();
            if(field!=null)
            Log.e("IllegalArgumentException--"+"field:"+field.getName()+"-value:"+field.getType());
        } catch (IllegalAccessException e) {
            Log.e(e.getMessage());
        }
    }

    /**
     *
     * @Title: getFieldValue
     * @Description: 返回对象属性的值
     * @param @param obj
     * @param @param key
     * @throws
     */
    public static Object getFieldValue(Object obj, String key) {
        Field field = null;
        Object result = null;
        try {
            field = obj.getClass().getDeclaredField(key);
            field.setAccessible(true);
            result = field.get(obj);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }
}
